using System;
using System.Configuration;


namespace BIUtilities.Publish.CustomConfiguration
{
    /// <summary>
    /// Collection of PPS Configuration elements
    /// </summary>
    public class PerformancePointElementCollection :
        System.Configuration.ConfigurationElementCollection, IUpdatableConfig
    {
        /// <summary>
        /// Create a new PPS Config element
        /// </summary>
        /// <returns></returns>
        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new PerformancePointElement();
        }

        /// <summary>
        /// Get the element's key for a pps item
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            PerformancePointElement ppsSection = (PerformancePointElement)element;
            return ppsSection.SourceMonitoringWebservice + ";" + ppsSection.MatchOnProperty + ";" + ppsSection.MatchOnValue;
        }

        /// <summary>
        /// Returns the type of collection
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        /// <summary>
        /// Returns the name of recognized children for this collection
        /// </summary>
        protected override string ElementName
        {
            get
            {
                return "performancePoint";
            }
        }

        /// <summary>
        /// Determins if a given element name is part of the children
        /// </summary>
        /// <param name="elementName"></param>
        /// <returns></returns>
        protected override bool IsElementName(string elementName)
        {
            if ((string.IsNullOrEmpty(elementName)) || (elementName != "performancePoint"))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Returns a PPS config element by index
        /// </summary>
        /// <param name="index">Index of item</param>
        /// <returns></returns>
        public PerformancePointElement this[int index]
        {
            get { return (PerformancePointElement)this.BaseGet(index); }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Makes this element editable
        /// </summary>
        /// <returns></returns>
        public override bool IsReadOnly()
        {
            return false;
        }


        #region IUpdatableConfig Members
        /// <summary>
        /// Creating a public accessor to get at the property values
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        object IUpdatableConfig.this[string propertyName]
        {
            get { return base[propertyName]; }
            set { base[propertyName] = value; }
        }
        #endregion
    }
}
